package com.itheima.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserMapper;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class TestDemo {

    private static SqlSessionFactory factory;
    private SqlSession sqlSession;
    private UserMapper userMapper;

    //修饰静态方法，只会在所有测试方法执行前执行一次
    @BeforeClass
    public static void init() throws IOException {
        //1.得到核心配置文件的输入流
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //2.创建工厂建造类 -> 工厂类 -> 会话类
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    //在每个测试方法前执行，执行多次
    @Before
    public void begin() {
        //创建会话
        sqlSession = factory.openSession();
        //创建接口的代理对象
        userMapper = sqlSession.getMapper(UserMapper.class);
    }
    /*
    查询所有用户
     */
    @Test
    public void findAll(){
        List<User> users = userMapper.findAll();
        System.out.println(users);
    }
    /*
    根据id查询
     */
    @Test
    public void findById(){
        List<User> users = userMapper.findById(2);
        System.out.println(users);
    }
    /*
    模糊查询
     */
    @Test
    public void findByName(){
        List<User> users = userMapper.findByName("%条%");
        System.out.println(users);
    }
    /*
    添加用户
     */
    @Test
    public void saveUser(){
        User user = new User();
        user.setUsername("球女");
        user.setPassword("957");
        int i = userMapper.saveUser(user);
        System.out.println("添加了"+i+"用户");
    }
    /*
    修改用户
    */
    @Test
    public void updateUser(){
        User user = new User();
        user.setId(2);
        user.setUsername("奶妈");
        user.setPassword("222");
        int i = userMapper.updateUser(user);
        System.out.println("修改了"+i+"用户");
    }
    /*
      保存并返回id
    * */
    @Test
    public void saveUserById(){
        User user = new User();
        user.setUsername("琴女");
        user.setPassword("333");
        int i = userMapper.saveUserById(user);
        System.out.println("保存后"+user);
        System.out.println("id是"+i);
    }
    @Test
    public void deleteUser(){
        int i = userMapper.deleteUser(16);
        System.out.println("删除了"+i+"用户");
    }


    /*
    多参数查询
     */
    @Test
    public void login(){
        User user = userMapper.login("李白","111");
        System.out.println(user);
    }
    /*
    动态SQL if

     */
    @Test
    public void findByCondition(){
        User user = new User();
        user.setUsername("%剑%");
        user.setPassword("%123%");
        List<User> users = userMapper.findByCondition(user);
        for (User user1 : users) {
            System.out.println(user1);
        }
    }
    /*
    动态Sql where标签
     */
    @Test
    public void findByCondition1(){
        User user = new User();
        user.setUsername("%剑%");
        user.setPassword("%123%");
        List<User> users = userMapper.findByCondition(user);
        for (User user1 : users) {
            System.out.println(user1);
        }
    }
    /*
    动态sql foreach list集合
     */
    @Test
    public void findUserInId(){
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        List<User> users = userMapper.findUserInId(ids);
        for (User user : users) {
            System.out.println(user);
        }
    }

    /*
     动态sql foreach 数组参数查询
     */
    @Test
    public void findUserInIds2(){
        Integer[] arr={3,4,6};
        List<User> users = userMapper.findUserInIds2(arr);
        System.out.println(users);
    }
    @Test
    public void update() {
        User user = new User();
        user.setId(15);
        user.setUsername("球女");
        //user.setPassword("123");

        userMapper.update(user);
    }

    /*
    分页
     */
    @Test
    public void testPageHelper(){

        PageHelper.startPage(1,3);

        List<User> users = userMapper.pageQuery();
        for (User user : users) {
            System.out.println(user);
        }
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        System.out.println("总条数："+pageInfo.getTotal());
        System.out.println("总页数："+pageInfo.getPages());
        System.out.println("当前页："+pageInfo.getPageNum());
        System.out.println("每页显示长度："+pageInfo.getPageSize());

    }

    @After
    public void end(){
        sqlSession.commit();
        sqlSession.close();
    }
}
